<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="com.gzsz.util.DBUtil" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("utf-8");
    String Name = request.getParameter("Name");
    String Password = request.getParameter("Password");
    String Role = request.getParameter("Role");

    // 空值检查
//    if (Name == null ||Password == null || Role == null) {
//        out.println("参数不能为空，请检查表单数据。");
//    } else {
        try {
            DBUtil dbUtil = new DBUtil();
            // 构造带参数的 SQL 语句
            String sql = "INSERT INTO yonghu(Name, Password, Role) VALUES ('" + Name + "', '" + Password + "', '" + Role + "');";
            // 3. 创建预编译命令对象
//            String sql = "INSERT INTO users (name, gender, age, phone, user_account) VALUES (?, ?, ?, ?, ?)";
//            preparedStatement = connection.prepareStatement(sql);
//
//// 设置参数
//            preparedStatement.setString(1, name);
//            preparedStatement.setString(2, gender);
//            preparedStatement.setInt(3, Integer.parseInt(age));
//            preparedStatement.setString(4, phone);
//            preparedStatement.setString(5, number);
            // 执行插入操作
            int i = dbUtil.executeUpdateBySQL(sql);
            if (i > 0) {
                response.sendRedirect("YongHu.jsp");
            } else {
                out.println("插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            out.println("发生错误：" + e.getMessage());
//        }
    }
%>